//
//  MTLiveProfileView.m
//  MeToo
//
//  Created by 俊松何 on 2018/7/19.
//  Copyright © 2018年 tianxie. All rights reserved.
//

#import "MTLiveProfileView.h"
#import "MTUserApiManager.h"
#import "MTLiveManagerListController.h"

@interface MTLiveProfileView ()
@property (weak, nonatomic) IBOutlet UIButton *reportButton;
@property (weak, nonatomic) IBOutlet UIButton *headerButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *singaerLable;
@property (weak, nonatomic) IBOutlet UIButton *atButton;
@property (weak, nonatomic) IBOutlet UIButton *founcsButton;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *fansnumberLable;

@property(nonatomic,strong) KLCPopup *pop;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heighConstant;
@property(nonatomic,strong) UIView *parentView;

@property (weak, nonatomic) IBOutlet UIButton *managerButton;

@property(nonatomic,strong) MTLiveUserInfo *userInfo;

@end

@implementation MTLiveProfileView

-(void)awakeFromNib
{
    [super awakeFromNib];
    self.headerButton.layer.masksToBounds = YES;
    self.headerButton.layer.cornerRadius = 40;
    
    self.bgView.layer.masksToBounds = YES;
    self.bgView.layer.cornerRadius = 8;
    
     self.parentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
     [self.parentView addSubview:self];
    
    UIGestureRecognizer *gester = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    
     UIGestureRecognizer *gester1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick1)];
    [self addGestureRecognizer:gester1];
    
    [self.parentView addGestureRecognizer:gester];
    
}

+(instancetype)liveProfileView
{
    NSBundle *bud = [NSBundle mainBundle];
    NSArray *arry = [bud loadNibNamed:NSStringFromClass(self) owner:nil options:nil];
    MTLiveProfileView *view = arry.lastObject;
    CGFloat W = WIDTH - 60;
    CGFloat H = 250;
    view.frame = CGRectMake(0, 0, W,  H);
    view.centerX = WIDTH/2.0;
    view.centerY = HEIGHT/2.0;
    
    return view;
}

-(void)show
{
    KLCPopup *newpop = [KLCPopup popupWithContentView:self];
    newpop.showType = KLCPopupShowTypeSlideInFromBottom;
    newpop.dismissType = KLCPopupDismissTypeSlideOutToBottom;
    newpop.maskType = KLCPopupMaskTypeDimmed;
    self.pop = newpop;
    if([[LTClient sharedClient].contactManager isMyselfWithUserId:_user.userId])
    {
        self.heighConstant.constant = 0;
    }else
    {
        self.heighConstant.constant = 50;
    }
    [newpop showAtCenter:CGPointMake(WIDTH/2.0, HEIGHT/2) inView:[[UIApplication sharedApplication].delegate window]];
     self.fansnumberLable.hidden = YES;
    [[MTUserApiManager manager] getUserInfo:_user.userId completion:^(MTUserModel *user, MTError *mterror) {
       if(mterror == nil)
       {
           if(user.info.fansCnt > 0)
           {
               self.fansnumberLable.hidden = NO;
               self.fansnumberLable.text = [NSString stringWithFormat:@"%ld粉丝",user.info.followCnt];
           }
       }
    }];
}

-(void)tapClick
{
    [UIView animateWithDuration:0.3 animations:^{
        self.y = HEIGHT;
    } completion:^(BOOL finished) {
        self.y = HEIGHT;
        [self.parentView removeFromSuperview];
    }];
}

-(void)tapClick1
{
    
}

-(void)showView:(UIView *)view
{
    self.userInfo = nil;
    self.managerButton.hidden = YES;
    
    if([[LTClient sharedClient].contactManager isMyselfWithUserId:_user.userId])
    {
        self.heighConstant.constant = 0;
    }else
    {
        self.heighConstant.constant = 50;
    }
    [view addSubview:self.parentView];
    self.y = HEIGHT;
    [UIView animateWithDuration:0.3 animations:^{
        self.centerY = HEIGHT/2;
    } completion:^(BOOL finished) {
        self.centerY = HEIGHT/2;
    }];
    self.fansnumberLable.hidden = YES;
    [[MTUserApiManager manager] getUserInfo:_user.userId completion:^(MTUserModel *user, MTError *mterror) {
        if(mterror == nil)
        {
            if(user.info.fansCnt > 0)
            {
                self.fansnumberLable.hidden = NO;
                self.fansnumberLable.text = [NSString stringWithFormat:@"%ld粉丝",user.info.followCnt];
            }
        }
    }];
    
    [[MTUserApiManager manager] getUserOperInfoMasterUserId:self.masterUserId targetUserId:_user.userId completion:^(MTLiveUserInfo *user, MTError *mterror) {
        if(mterror == nil)
        {
            self.userInfo = user;
            if((self.userInfo.selfInfo.userLiveStatus.isManager && !self.userInfo.targetInfo.userLiveStatus.isManager) || ([self.masterUserId isEqualToString:[LTClient sharedClient].currentUserId] && !self.ishost))
            {
                self.managerButton.hidden = NO;
            }else{
                self.managerButton.hidden = YES;
            }
            if([[LTClient sharedClient].contactManager isMyselfWithUserId:self.user.userId])
            {
                self.managerButton.hidden = YES;
                self.reportButton.hidden = YES;
            }
        }
    }];
}

-(void)setUser:(LTUser *)user
{
    _user = user;
    [self.headerButton sd_setImageWithURL:[NSURL URLWithString:user.avatar] forState:UIControlStateNormal placeholderImage:defaultImgae];
    self.nameLable.text = user.name;
    self.singaerLable.text = user.signature;
    if(user.signature.length == 0)
    {
        self.singaerLable.text = @"本宝宝暂时还没有想到个性的签名";
    }
    if(_user.follow)
    {
        [self.founcsButton setTitle:@"已关注" forState:UIControlStateNormal];
    }else
    {
        [self.founcsButton setTitle:@"关注" forState:UIControlStateNormal];
    }
    if([[LTClient sharedClient].contactManager isMyselfWithUserId:user.userId])
    {
        self.managerButton.hidden = YES;
        self.reportButton.hidden = YES;
    }else{
        self.reportButton.hidden = NO;
    }
}


- (IBAction)reportClick:(UIButton *)sender {
    
    [MBProgressHUD showMessage:@"举报成功"];
}

- (IBAction)atClick:(UIButton *)sender {
    
    if([self.delegate respondsToSelector:@selector(mtLiveProfileViewAtUser:)])
    {
        [self.delegate mtLiveProfileViewAtUser:self.user];
    }
    [self.pop dismiss:YES];
}

- (IBAction)foucsClick:(UIButton *)sender {
    
    [self attentionHandle];
    [[MTUserApiManager manager] followUserV2WithUserId:_user.userId follow:_user.follow completion:^(MTError *mterror) {
        if(mterror != nil)
        {
            [self attentionHandle];
        }
        if(_ishost && _user.follow)
        {
            if([self.delegate respondsToSelector:@selector(mtLiveProfileViewFocusHost)])
            {
                [self.delegate mtLiveProfileViewFocusHost];
            }
        }
    }];
}

- (IBAction)headerClick:(UIButton *)sender {
    
    if([self.delegate respondsToSelector:@selector(mtLiveProfileViewGoUserHomeUser:)])
    {
        [self.delegate mtLiveProfileViewGoUserHomeUser:self.user];
    }
    [self.pop dismiss:YES];
    
}



#pragma mark - 处理关注方法
- (void)attentionHandle{
    if (_user.follow) {
        _user.follow = NO;
         [self.founcsButton setTitle:@"关注" forState:UIControlStateNormal];
    } else {
        _user.follow = YES;
        [self.founcsButton setTitle:@"已关注" forState:UIControlStateNormal];
    }
}

- (IBAction)managerClick:(UIButton *)sender {
    
    
    UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *setManageraction = [UIAlertAction actionWithTitle:@"设为管理" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[MTUserApiManager manager] addLiveRoomManager:self.user.userId completion:^(MTError *mterror) {
           if(mterror == nil)
           {
               self.userInfo.targetInfo.userLiveStatus.isManager = YES;
               if([self.delegate respondsToSelector:@selector(mtLiveProfileViewSetMamnager:)])
               {
                   [self.delegate mtLiveProfileViewSetMamnager:self.user];
               }
               [MBProgressHUD showSuccess:@"设置成功"];
           }else{
               [MBProgressHUD showMessage:mterror.errorDescription];
           }
        }];
    }];
    
    UIAlertAction *cancelManageraction = [UIAlertAction actionWithTitle:@"取消管理" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
      
        [[MTUserApiManager manager] deleteLiveRoomManagertargetUserId:self.user.userId completion:^(MTError *mterror) {
           if(mterror == nil)
           {
               self.userInfo.targetInfo.userLiveStatus.isManager = NO ;
               if([self.delegate respondsToSelector:@selector(mtLiveProfileViewCancelMamnager:)])
               {
                   [self.delegate mtLiveProfileViewCancelMamnager:self.user];
               }
               [MBProgressHUD showSuccess:@"取消成功"];
           }else{
               [MBProgressHUD showMessage:mterror.errorDescription];
           }
        }];
    }];
    
    UIAlertAction *managerListaction = [UIAlertAction actionWithTitle:@"管理员列表" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        MTLiveManagerListController *managerVc = [[MTLiveManagerListController alloc] init];
        managerVc.type = 1;
        managerVc.liveUserId = self.masterUserId;
         managerVc.liveVc = self.targetVc;
        [self.targetVc.navigationController pushViewController:managerVc animated:true];
        [self tapClick];
    }];
    
    
    UIAlertAction *muteaction = [UIAlertAction actionWithTitle:@"禁言" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self sureBanned];
    }];
    
    UIAlertAction *cancleMuteaction = [UIAlertAction actionWithTitle:@"取消禁言" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[MTUserApiManager manager] deleteNoSpeaktargetUserId:self.user.userId masterUserId:self.masterUserId completion:^(MTError *mterror) {
            if(mterror == nil)
            {
                self.userInfo.targetInfo.userLiveStatus.isNoSpeaking = NO;
            }
        }];
    }];
    
    UIAlertAction *blackaction = [UIAlertAction actionWithTitle:@"拉黑" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self sureblack];
    }];

    
    UIAlertAction *cancleBlackaction = [UIAlertAction actionWithTitle:@"取消拉黑" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[MTUserApiManager manager] deleteBlackUsertargetUserId:self.user.userId masterUserId:self.masterUserId completion:^(MTError *mterror) {
           if(mterror == nil)
           {
               self.userInfo.targetInfo.userLiveStatus.isBlack = NO;
           }
        }];
    }];
    
    UIAlertAction *blacksListaction = [UIAlertAction actionWithTitle:@"黑名单列表" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        MTLiveManagerListController *managerVc = [[MTLiveManagerListController alloc] init];
        managerVc.type = 2;
        managerVc.liveUserId = self.masterUserId;
          managerVc.liveVc = self.targetVc;
        [self.targetVc.navigationController pushViewController:managerVc animated:true];
         [self tapClick];
    }];
    
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *muteListaction = [UIAlertAction actionWithTitle:@"禁言列表" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        MTLiveManagerListController *managerVc = [[MTLiveManagerListController alloc] init];
        managerVc.type = 3;
        managerVc.liveUserId = self.masterUserId;
         managerVc.liveVc = self.targetVc;
        [self.targetVc.navigationController pushViewController:managerVc animated:true];
         [self tapClick];
    }];
    
    //判断是主播
    if([self.masterUserId isEqualToString:[LTClient sharedClient].currentUserId])
    {
        if(self.userInfo.targetInfo.userLiveStatus.isManager)
        {
            [alterVc addAction:cancelManageraction];
        }else{
            [alterVc addAction:setManageraction];
        }
        [alterVc addAction:managerListaction];
        
        if(self.userInfo.targetInfo.userLiveStatus.isBlack)
        {
            [alterVc addAction:cancleBlackaction];
        }else{
            [alterVc addAction:blackaction];
        }
        [alterVc addAction:blacksListaction];
        
        
        if(self.userInfo.targetInfo.userLiveStatus.isNoSpeaking)
        {
            [alterVc addAction:cancleMuteaction];
        }else{
            [alterVc addAction:muteaction];
        }
        [alterVc addAction:muteListaction];
    }else{
        if(self.userInfo.targetInfo.userLiveStatus.isNoSpeaking)
        {
            [alterVc addAction:cancleMuteaction];
        }else{
            [alterVc addAction:muteaction];
        }
         [alterVc addAction:muteListaction];
        if(self.userInfo.targetInfo.userLiveStatus.isBlack)
        {
            [alterVc addAction:cancleBlackaction];
        }else{
            [alterVc addAction:blackaction];
        }
        [alterVc addAction:blacksListaction];
    }
    
    [alterVc addAction:cancleAction];
    [self.targetVc presentViewController:alterVc animated:YES completion:nil];
    
    
}


-(void)sureBanned
{
    UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认禁言TA？禁言后不能发送公聊和弹幕" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[MTLiveApiManager manager] muteUsermasterUserId:self.masterUserId targetUserId:self.user.userId completion:^(MTError *error) {
           if(error == nil)
           {
               self.userInfo.targetInfo.userLiveStatus.isNoSpeaking = YES;
               if([self.delegate respondsToSelector:@selector(mtLiveProfileViewMite:)])
               {
                   [self.delegate mtLiveProfileViewMite:self.user];
               }
                [MBProgressHUD showSuccess:@"禁言成功"];
           }else{
                [MBProgressHUD showMessage:error.errorDescription];
           }
            
        }];
        
        
    }];
    [alterVc addAction:action1];
    [alterVc addAction:action2];
    
    [self.targetVc presentViewController:alterVc animated:YES completion:nil];
    
    
}

-(void)sureblack
{
    UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"拉黑后,TA不能进入你的直播间。" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[MTLiveApiManager manager] kickOutUserLiveId:self.liveId targetUserId:self.user.userId completion:^(MTError *error) {
           if(error == nil)
           {
               if([self.delegate respondsToSelector:@selector(mtLiveProfileViewblack:)])
               {
                   [self.delegate mtLiveProfileViewblack:self.user];
               }
               [MBProgressHUD showSuccess:@"拉黑成功"];
           }else{
               [MBProgressHUD showMessage:error.errorDescription];
           }
        }];
    }];
    
    [alterVc addAction:action1];
    [alterVc addAction:action2];
    [self.targetVc presentViewController:alterVc animated:YES completion:nil];
    
}

-(void)managerMoreTip
{
    UIAlertController *alterVc = [UIAlertController alertControllerWithTitle:@"管理员已满" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alterVc addAction:action1];
    [self.targetVc presentViewController:alterVc animated:YES completion:nil];
    
}

//管理员只有禁言的操作

//主播有
//ismanger   isbland   isblack




@end
